<?php
class DynamicModuleAction extends BaseAction {
    
    //动态模块内页
    //用途：显示
    public function view(){
    	
    	$condition = $this->_get("condition");
    	$arr = explode("_",$condition);
    	if(count($arr) != 2){
    		header("HTTP/1.0 404 Not Found");
			$this->display('Public:404');
			return;
    	}
    	$cid = $arr[0];
    	$id = $arr[1];
    	$current_catalog = CatalogHelper::getObject("catalog_id={$cid}");
    	if(empty($current_catalog)){
			header("HTTP/1.0 404 Not Found");
			$this->display('Public:404');
			return;
		}
    	$dynamicModule = DynamicModuleHelper::getObject("dynamic_module_id={$current_catalog["dynamic_module_id"]}");
    	if(empty($dynamicModule)){
			header("HTTP/1.0 404 Not Found");
			$this->display('Public:404');
			return;
		}
		
		//网站基本配置
		$config = WebConfigHelper::getObject();
		$config["footer_cont"] = htmlspecialchars_decode($config["footer_cont"]);
		$config = CommonUtil::appendCustomerJSconfig($config);	//合并[脚本JS代码 & 客服JS代码]
		
    	$tableName = C("DB_PREFIX").$dynamicModule["table_name"];
    	$sql = "SELECT * FROM {$tableName} WHERE id={$id}";
		$info = DynamicModuleHelper::queryObject($sql);
    	if(empty($info)){
			header("HTTP/1.0 404 Not Found");
			$this->display('Public:404');
			return;
		}
		
		//SEO数据
    	$company_name = "";
		if(!empty($config["company_name"])){
			$company_name = "_".$config["company_name"];
		}
		$seo["title"] = $info["title"].$company_name;
		$seo["keywords"] = $info["seo_keywords"];
		$seo["description"] = $info["seo_description"];
		
    	$dynamicModuleFieldList = DynamicModuleFieldHelper::getList("dynamic_module_id={$current_catalog["dynamic_module_id"]}","order_num DESC");
    	//将$info对象里的数据进行处理
		foreach ($dynamicModuleFieldList as $field){
			$fieldName = $field["field_name"];
			//将多文件转换成数组-----------------------
			if($field["form_type"] == FormType::$MULTI_IMAGE || $field["form_type"] == FormType::$MULTI_FILE){
				$info[$fieldName] = explode("[]", $info[$fieldName]);
			}
			if($field["form_type"] == FormType::$EDITOR){
				$info[$fieldName] = htmlspecialchars_decode($info[$fieldName]);
			}
		}
    	
		$location = CatalogHelper::getLocation($current_catalog);	//当前页面导航位置
		$root_catalog = array();
		if($current_catalog["root_id"] != 0){	//根节点ID不为0，则取出根栏目
			$root_catalog = CatalogHelper::getObject("catalog_id=".$current_catalog["root_id"]);
		}else{
			$root_catalog = $current_catalog;
		}
		$root_cata_id = $root_catalog["catalog_id"];
		$current_cata_id = $current_catalog["catalog_id"];	//当前导航位置ID
		
		//---------------导航位置变量开始--------------------------------------------
		$this->assign("current_catalog",$current_catalog);
		$this->assign("root_catalog",$root_catalog);
		$this->assign("current_cata_id",$current_cata_id);
		$this->assign("root_cata_id",$root_cata_id);
		$this->assign("location",$location);
		//---------------导航位置变量结束--------------------------------------------
		
		//浏览量+1
		$sql = "UPDATE {$tableName} SET view_num=view_num+1 WHERE id={$id}";
		DynamicModuleHelper::updateSQL($sql);
		
		$this->assign("info",$info);
		$this->assign("config",$config);
		$this->assign("seo",$seo);
		$this->assign("template_path",CommonUtil::getDir()."/themes/".$config["template_name"]);
		
    	$this->display(CommonUtil::getTemplatePath().$current_catalog["view_module_filepath"]);
    }
    
    
}